﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Media.Animation;

namespace Jade.Tools.SL.Controls.Transitionnals
{
    /// <summary>
    /// Translate Effect UI Element
    /// </summary>
    public partial class TranslateTransitionElement : UserControl
    {
        private ContentPresenter newContentPresenter;
        private bool isTransitioning;

        #region Ctor
        public TranslateTransitionElement()
        {
            InitializeComponent();
            newContentPresenter = new ContentPresenter();
        }
        #endregion

        #region Public Methods

        public object ContentTransition
        {
            get { return (object)GetValue(ContentTransitionProperty); }
            set { SetValue(ContentTransitionProperty, value); }
        }

        public static readonly DependencyProperty ContentTransitionProperty =
            DependencyProperty.Register("ContentTransition",
                typeof(object),
                typeof(TranslateTransitionElement),
                new PropertyMetadata(null, OnContentTransitionChanged));

        public bool IsGoForward
        {
            get { return (bool)GetValue(IsGoForwardProperty); }
            set { SetValue(IsGoForwardProperty, value); }
        }

        public static readonly DependencyProperty IsGoForwardProperty =
            DependencyProperty.Register("IsGoForward", typeof(bool), typeof(TranslateTransitionElement), null);

        #endregion

        #region Private Methods

        private static void OnContentTransitionChanged(object element, DependencyPropertyChangedEventArgs e)
        {
            TranslateTransitionElement te = element as TranslateTransitionElement;
            if (te != null)
            {
                te.BeginTransition();
            }
        }

        private void BeginTransition()
        {
            if (ContentTransition != null)
            {
                if (CtCenter.Content != null)
                {
                    if (!isTransitioning)
                    {
                        double direction = 1;
                        if (IsGoForward) { direction = 1; } else { direction = -1; }

                        //Translate Transform CtCenter
                        TranslateTransform tCenter = new TranslateTransform();
                        tCenter.X = 0;
                        tCenter.Y = 0;
                        CtCenter.RenderTransform = tCenter;
                        CtCenter.RenderTransformOrigin = new Point(0.5, 0.5);

                        //Translate Transform NewContentPresenter
                        TranslateTransform tOld = new TranslateTransform();
                        tOld.X = direction * this.ActualWidth;
                        tOld.Y = 0;
                        newContentPresenter.RenderTransform = tOld;
                        newContentPresenter.RenderTransformOrigin = new Point(0.5, 0.5);
                        newContentPresenter.Content = ContentTransition;
                        GrdMain.Children.Add(newContentPresenter);

                        //Animation oldContentAnimation
                        DoubleAnimation oldContentAnimation = new DoubleAnimation();
                        oldContentAnimation.To = -direction * this.ActualWidth;
                        oldContentAnimation.Duration = new Duration(TimeSpan.FromMilliseconds(1200));

                        ElasticEase ee = new ElasticEase();
                        ee.EasingMode = EasingMode.EaseOut;
                        ee.Oscillations = 1;
                        oldContentAnimation.EasingFunction = ee;

                        Storyboard stOldContentAnimation = new Storyboard();
                        Storyboard.SetTargetProperty(oldContentAnimation, new PropertyPath(TranslateTransform.XProperty));
                        Storyboard.SetTarget(oldContentAnimation, tCenter);
                        stOldContentAnimation.Children.Add(oldContentAnimation);
                        stOldContentAnimation.Begin();


                        //Animation newContentAnimation
                        DoubleAnimation newContentAnimation = new DoubleAnimation();
                        newContentAnimation.To = 0;
                        newContentAnimation.Duration = new Duration(TimeSpan.FromMilliseconds(1200));

                        ElasticEase ee1 = new ElasticEase();
                        ee1.EasingMode = EasingMode.EaseOut;
                        ee1.Oscillations = 1;
                        newContentAnimation.EasingFunction = ee1;

                        Storyboard stNewContentAnimation = new Storyboard();
                        Storyboard.SetTargetProperty(newContentAnimation, new PropertyPath(TranslateTransform.XProperty));
                        Storyboard.SetTarget(newContentAnimation, tOld);
                        stNewContentAnimation.Children.Add(newContentAnimation);
                        stNewContentAnimation.Completed += stNewContentAnimation_Completed;
                        stNewContentAnimation.Begin();

                        isTransitioning = true;

                    }
                }
                else
                {
                    CtCenter.Content = ContentTransition;
                    TranslateTransform tCenter = new TranslateTransform();
                    tCenter.X = 0;
                    tCenter.Y = 0;
                    CtCenter.RenderTransform = tCenter;
                    CtCenter.RenderTransformOrigin = new Point(0.5, 0.5);
                }
            }
        }

        void stNewContentAnimation_Completed(object sender, EventArgs e)
        {
            isTransitioning = false;

            CtCenter.ClearValue(ContentPresenter.RenderTransformProperty);
            newContentPresenter.ClearValue(ContentPresenter.RenderTransformProperty);

            newContentPresenter.Content = null;
            GrdMain.Children.Remove(newContentPresenter);
            CtCenter.Content = ContentTransition;

            CtCenter.ClearValue(ContentPresenter.RenderTransformProperty);
            newContentPresenter.ClearValue(ContentPresenter.RenderTransformProperty);

            TranslateTransform tCenter = new TranslateTransform();
            tCenter.X = 0;
            tCenter.Y = 0;
            CtCenter.RenderTransform = tCenter;
            CtCenter.RenderTransformOrigin = new Point(0.5, 0.5);

        }

        #endregion
    }
}
